package com.egg.ylt.activity;

import android.os.Bundle;
import android.text.Editable;
import android.text.TextWatcher;
import android.view.View;
import android.widget.EditText;
import android.widget.ImageView;
import android.widget.RelativeLayout;
import android.widget.TextView;

import com.egg.ylt.R;
import com.egg.ylt.Utils.Constants;
import com.egg.ylt.Utils.CustomUtils;
import com.egg.ylt.Utils.StatusBarUtil;
import com.egg.ylt.Utils.StringUtil;
import com.egg.ylt.common.ContainsEmojiEditText;
import com.egg.ylt.presenter.impl.OpinionPresenterImpl;
import com.egg.ylt.view.IOpinionView;
import com.yonyou.framework.library.base.BaseActivity;
import com.yonyou.framework.library.bean.ErrorBean;
import com.yonyou.framework.library.common.utils.ToastUtil;

import butterknife.BindView;
import butterknife.OnClick;

public class ACT_OpinionCoupleBack extends BaseActivity<OpinionPresenterImpl> implements IOpinionView {


    @BindView(R.id.include_rl_view)
    RelativeLayout mIncludeRlView;
    @BindView(R.id.include_iv_back)
    ImageView includeIvBack;
    @BindView(R.id.include_tv_title)
    TextView includeTvTitle;
    @BindView(R.id.edit_user_opinion)
    ContainsEmojiEditText editUserOpinion;
    @BindView(R.id.tv_text_number)
    TextView tvTextNumber;
    @BindView(R.id.btn_commit)
    TextView btnCommit;

    @Override
    protected void getBundleExtras(Bundle extras) {

    }

    @Override
    protected int getContentViewLayoutID() {
        return R.layout.act_opinion_couple_back;
    }

    @Override
    protected View getLoadingTargetView() {
        return null;
    }

    @Override
    protected void initViewsAndEvents() {

        StatusBarUtil.setStatusBarColor(this, R.color.ylt_whit);
        StatusBarUtil.StatusBarLightMode(this);
        CustomUtils.setImmersiveStatusBar(mContext, mIncludeRlView);
        //设置title
        includeTvTitle.setText("意见反馈");

        //意见反馈字数监听
        editUserOpinion.addTextChangedListener(mTextWatcher);
    }

    TextWatcher mTextWatcher = new TextWatcher() {
        private CharSequence temp;
        private int editStart;
        private int editEnd;

        @Override
        public void beforeTextChanged(CharSequence s, int arg1, int arg2,
                                      int arg3) {
            temp = s;
        }

        @Override
        public void onTextChanged(CharSequence s, int arg1, int arg2,
                                  int arg3) {
            tvTextNumber.setText(s.length() + "字");
        }

        @Override
        public void afterTextChanged(Editable s) {
            editStart = editUserOpinion.getSelectionStart();
            editEnd = editUserOpinion.getSelectionEnd();
            if (temp.length() > 100) {
                ToastUtil.makeText(mContext, "限制100字！", false);
                s.delete(editStart - 1, editEnd);
                int tempSelection = editStart;
                editUserOpinion.setText(s);
                editUserOpinion.setSelection(tempSelection);
            }
        }
    };

    @Override
    protected boolean isApplyStatusBarTranslucency() {
        return false;
    }

    @Override
    protected boolean isBindEventBusHere() {
        return false;
    }

    @Override
    protected boolean toggleOverridePendingTransition() {
        return false;
    }

    @Override
    protected TransitionMode getOverridePendingTransitionMode() {
        return null;
    }

    @Override
    protected boolean isApplyKitKatTranslucency() {
        return false;
    }

    @Override
    public void showBusinessError(ErrorBean error) {

    }

    @OnClick({R.id.include_iv_back, R.id.btn_commit})
    public void onClick(View view) {
        switch (view.getId()) {
            case R.id.include_iv_back://返回键
                finish();
                break;
            case R.id.btn_commit://保存反馈意见
                String opinionText = editUserOpinion.getText().toString().trim();
                if (StringUtil.isEmpty(opinionText)) {
                    ToastUtil.makeText(mContext, "请先输入意见", false);
                    return;
                }
                mPresenter.postOpinionContent(Constants.TOKEN, Constants.COMPANYID, opinionText, "0");
                break;
        }
    }

    @Override
    public void saveOpinionSucceed() {
        ToastUtil.makeText(mContext, "反馈信息成功", false);
        finish();
    }
}
